//init
var tab_index = parseInt(getParameterByName("t"));
if(!$.isNumeric(tab_index)) tab_index = 0;

// scrollable
$("div.scrollable").not(".alt").scrollable({circular: true}).autoscroll(4000).navigator();
// miembros
$("div.scrollable.alt").scrollable({circular: true, prev: ".prev_alt", next: ".next_alt"});
$("div.detail").eq(0).show();
$("div.subitem").on("click", function() {
	var $this = $(this),
		index = $this.index() + (7 * ($this.parent().index("div.item:not(.cloned)")));
	if($this.hasClass("current")) return;
	$("div.subitem").removeClass("current");
	$this.addClass("current");
	$("div.detail").hide().eq(index).fadeIn();
});

// mentores
$("div.mentor").on("click", function() {
	var $this = $(this),
		index = $this.index("div.mentor");
	$("div.mentor").removeClass("active").filter(this).addClass("active");
	if(!$("div.mentor_detail:visible").length) $("div.mentor_detail").hide().eq(index).stop(true).slideDown();
	else $("div.mentor_detail").hide().eq(index).show();
});
$("div.mentor_detail").children("a.close").on("click", function() {
	$(this).parent().stop(true).slideUp();
});

// salas
$("a.btn_info").on("click", function() {
	var index = parseInt($(this).data("index"));
	$("tr.room_detail").hide().eq(index).slideDown();
});

//tabs
$("ul.tabs").not(".disabled").tabs("div.panes > div", {tabs: "li:not(.disabled)", effect:"fade", initialIndex:tab_index});
$("ul.tabs, #nav").find("a[rel]").overlay({top: "center", mask: {color: '#000000', loadSpeed: 200, opacity: 0.9}});
$("a.feedback").overlay({top: "center", mask: {color: '#000000', loadSpeed: 200, opacity: 0.9}});
$("a.feedback2").overlay({top: "center", mask: {color: '#000000', loadSpeed: 200, opacity: 0.9}});
$(".resizeIMG a[rel]").overlay({top: "center", mask:{color:"#000000", loadSpeed: 200, opacity:0.9}, onBeforeLoad: function() {
	var $lbox = this.getOverlay(),
		$trigger = this.getTrigger();
	$lbox.find("img").attr("src", $trigger.data("src"));
}});

$("form").on("submit", function(e) {
	e.preventDefault();
	var $form = $(this),
		isValid = true;
	$form.find("input:text, textarea").each(function() {
		var val = $(this).val();
		if(!val || ($(this).attr("name") === "email" && !val.match("^(.+)@(.+)$"))) isValid = false;
		if(!val || ($(this).attr("name") === "T1" && !val.match("^(.+)@(.+)$"))) isValid = false;
	});
	if(!isValid) return;
	var url = $form.attr("action");
	$.post(url, $(this).serialize(), function(data) {
		$form.replaceWith(data);
	});
});

$("#nav, #menu").on({
	mouseenter: function() { $(this).children("ul").stop(true).slideDown();	},
	mouseleave: function() { $(this).children("ul").hide(); }
}, "li");

// window
$(window).on({
resize: function() {
	var margintop = ($("div.brand").innerHeight() - $("div.brand_text").innerHeight()) / 2;
	if(margintop < 0) margintop = 0;
	$("div.brand_text").css("margin-top", margintop);
	$("#w_slider").height(Math.floor($("#w_slider").width() * 0.2515625));
	$("div.scrollable").each(function() {
		$(this).data("scrollable").seekTo($(this).data("scrollable").getIndex(), 0);
	});
},
load: function() {
	$(this).trigger("resize");
}}).trigger("resize");

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}